﻿using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleRabbitMQTemplate
{
	/// <summary>
	/// 
	/// </summary>
	internal class BasicUse
	{
		public async void show()
		{
			#region
			string dir=AppDomain.CurrentDomain.BaseDirectory;
			string productName=Path.GetDirectoryName(dir);
			ConnectionFactory connectionFactory = new ConnectionFactory();


			connectionFactory.HostName = UrlConfig.Url;
			connectionFactory.UserName=UrlConfig.User;
			connectionFactory.Password=UrlConfig.Password;
			connectionFactory.Port=5672;

			using (IConnection connection =  connectionFactory.CreateConnectionAsync().Result)
			{
				using (IChannel channel= connection.CreateChannelAsync().Result)
				{
					//声明
					await channel.QueueDeclareAsync(queue:"OnlyProducerMessage",durable:false,exclusive:false,autoDelete:false,null);
					await channel.ExchangeDeclareAsync(exchange: "OnlyProducerMessageExchange", type: ExchangeType.Direct,durable:true,autoDelete:false,null);
					await channel.QueueBindAsync(queue: "OnlyProducerMessage", exchange: "OnlyProducerMessageExchange", routingKey:string.Empty,arguments:null);

					//准备消息
					int i = 0;
					while (true) {

						byte[] bytes = Encoding.UTF8.GetBytes("message");
						Console.WriteLine("添加一条-------------消息!");
						await channel.BasicPublishAsync(exchange: "OnlyProducerMessageExchange",
							routingKey: string.Empty ,
							mandatory: false,
							body: bytes);
						Thread.Sleep(500);
						i ++;
						Console.WriteLine("消息已经发送!");
					}
					
				}
			}
			#endregion
		}

	}
}
